import { defineStore } from 'pinia'
import type { Course, CourseChapter, CourseHour, CourseAttachment } from '@/types/course'

interface CourseState {
  courses: Course[]
  currentCourse: Course | null
  chapters: CourseChapter[]
  hours: CourseHour[]
  attachments: CourseAttachment[]
  total: number
  loading: boolean
}

export const useCourseStore = defineStore('course', {
  state: (): CourseState => ({
    courses: [],
    currentCourse: null,
    chapters: [],
    hours: [],
    attachments: [],
    total: 0,
    loading: false
  }),

  getters: {
    getCourses: (state) => state.courses,
    getCurrentCourse: (state) => state.currentCourse,
    getChapters: (state) => state.chapters,
    getHours: (state) => state.hours,
    getAttachments: (state) => state.attachments,
    getTotal: (state) => state.total,
    isLoading: (state) => state.loading
  },

  actions: {
    // 获取课程列表
    async fetchCourses(params?: any) {
      this.setLoading(true)
      try {
        // TODO: 调用获取课程列表API
        // const response = await getCoursesApi(params)
        // this.setCourses(response.data.data)
        // this.setTotal(response.data.total)
        // return response
      } catch (error) {
        throw error
      } finally {
        this.setLoading(false)
      }
    },

    // 获取课程详情
    async fetchCourse(id: number) {
      this.setLoading(true)
      try {
        // TODO: 调用获取课程详情API
        // const response = await getCourseApi(id)
        // this.setCurrentCourse(response.data)
        // return response
      } catch (error) {
        throw error
      } finally {
        this.setLoading(false)
      }
    },

    // 创建课程
    async createCourse(courseData: Partial<Course>) {
      try {
        // TODO: 调用创建课程API
        // const response = await createCourseApi(courseData)
        // await this.fetchCourses()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 更新课程
    async updateCourse(id: number, courseData: Partial<Course>) {
      try {
        // TODO: 调用更新课程API
        // const response = await updateCourseApi(id, courseData)
        // await this.fetchCourse(id)
        // return response
      } catch (error) {
        throw error
      }
    },

    // 删除课程
    async deleteCourse(id: number) {
      try {
        // TODO: 调用删除课程API
        // const response = await deleteCourseApi(id)
        // await this.fetchCourses()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 获取课程章节
    async fetchChapters(courseId: number) {
      try {
        // TODO: 调用获取课程章节API
        // const response = await getChaptersApi(courseId)
        // this.setChapters(response.data)
        // return response
      } catch (error) {
        throw error
      }
    },

    // 获取课程课时
    async fetchHours(courseId: number) {
      try {
        // TODO: 调用获取课程课时API
        // const response = await getHoursApi(courseId)
        // this.setHours(response.data)
        // return response
      } catch (error) {
        throw error
      }
    },

    // 获取课程附件
    async fetchAttachments(courseId: number) {
      try {
        // TODO: 调用获取课程附件API
        // const response = await getAttachmentsApi(courseId)
        // this.setAttachments(response.data)
        // return response
      } catch (error) {
        throw error
      }
    },

    // 设置课程列表
    setCourses(courses: Course[]) {
      this.courses = courses
    },

    // 设置当前课程
    setCurrentCourse(course: Course) {
      this.currentCourse = course
    },

    // 设置章节列表
    setChapters(chapters: CourseChapter[]) {
      this.chapters = chapters
    },

    // 设置课时列表
    setHours(hours: CourseHour[]) {
      this.hours = hours
    },

    // 设置附件列表
    setAttachments(attachments: CourseAttachment[]) {
      this.attachments = attachments
    },

    // 设置总数
    setTotal(total: number) {
      this.total = total
    },

    // 设置加载状态
    setLoading(loading: boolean) {
      this.loading = loading
    },

    // 重置状态
    resetState() {
      this.courses = []
      this.currentCourse = null
      this.chapters = []
      this.hours = []
      this.attachments = []
      this.total = 0
      this.loading = false
    }
  }
})